package com.dankegongyu.lib.common.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.ScrollView;

/**
 * 解决 ScrollView 嵌套 RecyclerView 时滑动惯性问题
 * refs: http://www.itnose.net/detail/6319751.html
 *
 * @author WuPuquan
 * @version 1.0
 * @since 2017/9/19 18:17
 */
public class DKScrollView extends ScrollView {

    private float xDistance, yDistance, xLast, yLast;
    private float rawX, rawY;
    private int mTouchSlop;

    public DKScrollView(Context context) {
        super(context);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    public DKScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    public DKScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent e) {
        final float x = e.getX();
        final float y = e.getY();
        switch (e.getAction()) {
            case MotionEvent.ACTION_DOWN:
                xDistance = yDistance = 0f;
                xLast = e.getX();
                yLast = e.getY();

                rawX = e.getRawX();
                rawY = e.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                xDistance += Math.abs(x - xLast);
                yDistance += Math.abs(y - yLast);
                xLast = x;
                yLast = y;
                if (xDistance >= yDistance) {
                    return false;
                }

                float moveY = e.getRawY();
                if (Math.abs(moveY - rawY) > mTouchSlop) {
                    return true;
                }
                break;
        }
        return super.onInterceptTouchEvent(e);
    }

    /**
     * 滚动的回调接口
     */
    public interface OnScrollListener {
        //        /**
//         * 回调方法， 返回MyScrollView滑动的Y方向距离
//         * @param scrollY
//         */
//        public void onScroll(int scrollY);
        void onScrollChanged(DKScrollView scrollView, int x, int y, int oldx, int oldy);
    }

    private OnScrollListener onScrollListener = null;
    // 主要是用在用户手指离开MyScrollView，MyScrollView还在继续滑动，我们用来保存Y的距离，然后做比较
    private int lastScrollY;

    public void setOnScrollListener(OnScrollListener onScrollListener) {
        this.onScrollListener = onScrollListener;
    }

    @Override
    protected void onScrollChanged(int x, int y, int oldx, int oldy) {
        super.onScrollChanged(x, y, oldx, oldy);
        if (onScrollListener != null) {
            onScrollListener.onScrollChanged(this, x, y, oldx, oldy);
        }
    }

    @Override
    public void requestChildFocus(View child, View focused) {
        // 解决自动滚动问题 http://jcodecraeer.com/a/anzhuokaifa/androidkaifa/2018/0130/9270.html
    }
}
